/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "EEPROM.h"
#include "Gray_Sensor.h"
#include "PID.h"
#include "encoder.h"
#include "main.h"
#include "ti_msp_dl_config.h"
uint8_t oled_buffer[32];
 

int main(void) {

  char buffer[128];
  float t = 0;

  SYSCFG_DL_init();
  SYSCFG_DL_TIMER_0_init();
  SysTick_Init();
  // 清除串口中断标志
  NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
  // 使能串口中断
  NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
  NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
  Init_I2c_Encoder_Reg(&enctest, I2C_ENCODER_INST, 0x01);
  // 使能定时器0的ZERO中断
  // DL_TimerG_enableInterrupt(TIMER_0_INST, DL_TIMERG_INTERRUPT_ZERO_EVENT);

  // 启动定时器0的计数器，开始计数
  DL_TimerG_startCounter(TIMER_0_INST);
  NVIC_ClearPendingIRQ(TIMER_0_INST_INT_IRQN);
  LCD_Init();
  delay_ms(1);
  EEPROM_Init();
  //W25QXX_first_Init();
  key_init();
  //buzzer_on();
  //No_MCU_Ganv_Sensor_Init(&sensor, white, black);
  
  // 使能ADC转换
  //DL_ADC12_enableConversions(ADC0_INST);
  // 软件触发ADC开始转换
  //DL_ADC12_startConversion(ADC0_INST);
  // MPU6050_Init();
  // 感为传感器初始化
  // No_MCU_Ganv_Sensor_Init_Frist(&sensor);
  // Get_Anolog_Value(&sensor,Anolog);
  // uart0_printf("Anolog
  // %d-%d-%d-%d-%d-%d-%d-%d\r\n",Anolog[0],Anolog[1],Anolog[2],Anolog[3],Anolog[4],Anolog[5],Anolog[6],Anolog[7]);
  

  while (1) {
    
    //lcd_proc();
    // set_target_speed_B(80.0f);
    //uart0_printf("%d\r\n", get_tick());
    //uart0_printf("%d\r\n",DL_GPIO_readPins(KEY_KEY1_PORT,KEY_KEY1_PIN )? 1 : 0);
    //test_eeprom_rw();
    scheduler_run();
  }
}

void UART_0_INST_IRQHandler(void) {
  switch (DL_UART_getPendingInterrupt(UART_0_INST)) {
  case DL_UART_IIDX_RX: {
    char c = DL_UART_Main_receiveData(UART_0_INST);
    if (c == '\n') { // 只对\n处理
      uart_rx_buf[uart_rx_idx] = 0;
      parse_uart_command((char *)uart_rx_buf, &PID_encoder_BSPD);
      uart_rx_idx = 0;
    } else if (c != '\r' && uart_rx_idx < UART_BUF_LEN - 1) {
      uart_rx_buf[uart_rx_idx++] = c;
    }
  } break;
  default:
    break;
  }
}

void TIMER_0_INST_IRQHandler(void) {
  switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) {
  case DL_TIMERG_IIDX_ZERO:
    button_ticks(); // 按键处理
    //uart0_printf("%d\n",get_tick());
    DL_TimerG_clearInterruptStatus(TIMER_0_INST,
                                   DL_TIMERG_INTERRUPT_ZERO_EVENT);
    break;

  default:
    break;
  }
}
// 函数：获取用户归一化数据
// 参数：sensor：传感器指针；result：结果指针
// 返回值：unsigned char：成功返回1，失败返回0
// 判断传感器是否正常
